# _*_ encoding: utf-8 _*_
# __author__ = 'lx'
import datetime

from moviepy.editor import *
# cfg = {}
# path =  "biaos.mp4"
# W = 848
# H = 480
# # 生成视频剪辑
#
# video_src = VideoFileClip(path)
#
# video_clip = video_src.crop(y1=video_src.size[1]*0.2, height=video_src.size[0]*H / W).set_position((0.2,0),relative=True)
# W,H = video_clip.size
# W,H = W*2,H*2
# video_clip = video_clip.resize(2*0.8)
# img_clip = ImageClip("pic1.jpg").set_duration(video_clip.end).set_position((0,0.4*H)).resize((W*0.2,H*0.2))
# text0 = TextClip("Bilibili",fontsize=20,color='DarkRed',font='fonts/simsun.ttc').set_position((15,30)).set_duration(video_clip.end)
#
# text1 = TextClip("标哥锻炼(⊙o⊙)",fontsize=40,color='black',font='fonts/simsun.ttc').set_duration(video_clip.end/5)
# text2 = TextClip("标哥肌肉好强（。＾▽＾）",fontsize=40,color='black',font='fonts/simsun.ttc').set_duration(video_clip.end/5)
# text3 = TextClip("标哥动作真标准♥",fontsize=40,color='black',font='fonts/simsun.ttc').set_duration(video_clip.end/5)
# text4 = TextClip("标哥怎么这么棒(๑•̀ㅂ•́)و✧",fontsize=40,color='black',font='fonts/simsun.ttc').set_duration(video_clip.end/5)
# text5 = TextClip("标哥带我(♥ ω ♥)",fontsize=40,color='black',font='fonts/simsun.ttc').set_duration(video_clip.end/5)
# text = concatenate_videoclips([text1,text2,text3,text4,text5]).set_position((0,0.8*H))
# #
# audio = AudioFileClip("mzt.mp3").subclip(0, video_clip.end)
#
# final_clip = CompositeVideoClip([video_clip,img_clip,text,text0,text],size=(W,H),bg_color=[255,255,255]).set_audio(audio).volumex(0.01)
# final_clip.write_videofile("final.mp4", fps=24, codec='mpeg4')
from constant import VIDEO_RESULTS, FONT_PATH


def template_on_video(text_list,video_path,img_path,bgm_path,img_pos=0.25,volumex=0.01,text_font=FONT_PATH+'/simsun.ttc',text_color='black',text_font_size=40):
    # 评论列表需要根据视频时长处理
    b = 0.75
    video_clip = VideoFileClip(video_path).set_position((1-b,0),relative=True)
    W, H = video_clip.size
    video_clip = video_clip.resize(b)
    img_clip = ImageClip(img_path)
    img_resize = (1-b)*W/(3*img_clip.size[0])
    img_clip = img_clip.set_duration(video_clip.end).set_position(((1-b)*W/3, img_pos * H)).resize(img_resize)

    text_clips = []
    # 自动切分
    for i in text_list:
        # 添加换行
        text_clips.append(
        TextClip(i, fontsize=text_font_size, color=text_color, font=text_font,method='caption',size=(0.95*W,None)).set_duration(video_clip.end / len(text_list))
        )
    text_clips = concatenate_videoclips(text_clips).set_position((0.05*W,(b+0.05)*H))

    audio = AudioFileClip(bgm_path).subclip(0, video_clip.end)
    final_clip = CompositeVideoClip([video_clip, img_clip, text_clips], size=(W, H),
                                    bg_color=[255, 255, 255]).set_audio(audio).volumex(volumex)
    filename = datetime.datetime.now().strftime('%Y%m%d%H%M')
    path = VIDEO_RESULTS + '/V' + filename + '.mp4'
    final_clip.write_videofile(path, fps=24, codec='libx264')       # bitrate='300k'
    return 'media/results/video'+'/V' + filename + '.mp4'


if __name__ == '__main__':
    text_list = ['标哥锻炼(⊙o⊙)','怎么这么棒(๑•̀ㅂ•́)و✧','肌肉好强']
    template_on_video(text_list,"../media/material/video/test1.mp4","../static/material/img/youtube_logo.png","../media/material/music/mzt.mp3")